﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_gescliente : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Utilizador util2;
        public Cliente c = null;
        public Animal na = null;
        
        public frm_gescliente(Utilizador vet)
        {
            this.util2 = vet;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }


        private void cmdgc_pesquisar_Click(object sender, EventArgs e)
        {
            try{
                pesquisa_cliente();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdgc_inscliente_Click(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT MAX(id_cliente) FROM Cliente";
            SqlCommand command2 = con.CreateCommand();
            command2.CommandText = "SELECT Count(*) FROM Cliente";
            int num1 = (Int32)command2.ExecuteScalar();
            int num = 0;
            if (num1 == 0)
                num = 1;
            else
            {
                num = (Int32)command1.ExecuteScalar();
                num++;
            }

            con.Close();
            Cliente n = new Cliente(num, "novo nome", "nova morada", 0, "nova localidade", 0, "novo email");

            frm_icliente frm1 = new frm_icliente(util2, n, "novo");
            frm1.ShowDialog();
        }

        private void cmdgc_voltar_Click(object sender, EventArgs e)
        {
            frm_gesclinica frm2 = new frm_gesclinica(util2);
            this.Close();
            frm2.Show();
        }

        private void cmdgc_as_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                Cliente m = new Cliente(Convert.ToInt32(dataGridView1["id_cliente", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["morada", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["cod_postal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["localidade", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["telemovel", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["email", dataGridView1.CurrentRow.Index].Value.ToString());

                frm_icliente frm3 = new frm_icliente(util2, m, "editar");
                frm3.ShowDialog();            
            }
        }

        private void pesquisa_cliente()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int id = util2.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome LIKE '%" + txtgc_nome.Text + "%' and id_veterinario = "+ id +" and oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();
            
            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_cliente"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["morada"].ToString();
                this.dataGridView1[3, i].Value = readatrib["cod_postal"].ToString();
                this.dataGridView1[4, i].Value = readatrib["localidade"].ToString();
                this.dataGridView1[5, i].Value = readatrib["telemovel"].ToString();
                this.dataGridView1[6, i].Value = readatrib["email"].ToString();

            }
            
            con.Close();
        }

        private void todos_clientes()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int id = util2.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE id_veterinario = " + id + " and oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_cliente"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["morada"].ToString();
                this.dataGridView1[3, i].Value = readatrib["cod_postal"].ToString();
                this.dataGridView1[4, i].Value = readatrib["localidade"].ToString();
                this.dataGridView1[5, i].Value = readatrib["telemovel"].ToString();
                this.dataGridView1[6, i].Value = readatrib["email"].ToString();

            }

            con.Close();
        }

        private void cmdgc_es_Click(object sender, EventArgs e)
        {
            try
            {
                var op = MessageBox.Show("Tem a certeza que pretende remover o cliente selecionado?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (op == DialogResult.Yes)
                {
                    if (this.dataGridView1.RowCount != 0)
                    {
                        int i = this.dataGridView1.CurrentRow.Index;
                        con = new System.Data.SqlClient.SqlConnection();
                        con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                        con.Open();

                        string temp_id = this.dataGridView1.Rows[i].Cells["id_cliente"].Value.ToString();
                        //marcar cliente como oculto ao invés de eliminar
                        SqlCommand command99 = con.CreateCommand();
                        command99.CommandText = "Update Cliente Set oculto = 1 WHERE id_cliente = '"+ temp_id +"'";
                        SqlDataReader readatrib99 = command99.ExecuteReader();
                        con.Close();
                        //marcar animais desse cliente como ocultos
                        con.Open();
                        SqlCommand command98 = con.CreateCommand();
                        command98.CommandText = "Update Animais Set oculto = 1 WHERE id_cliente = '" + temp_id + "'";
                        SqlDataReader readatrib98 = command98.ExecuteReader();
                        con.Close();
                        //marcar explorações desse cliente como ocultas
                        con.Open();
                        SqlCommand command97 = con.CreateCommand();
                        command97.CommandText = "Update Exploracao Set oculto = 1 WHERE id_cliente = '" + temp_id + "'";
                        SqlDataReader readatrib97 = command97.ExecuteReader();
                        con.Close();

                        this.dataGridView1.Rows.Clear();
                    }
                    else
                        MessageBox.Show("Primeiro tem de selecionar o cliente!");
                    }
                }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdgc_todos_Click(object sender, EventArgs e)
        {
            try
            {
                todos_clientes();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdgc_ia_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT MAX(id_animal) FROM Animais";
                SqlCommand command2 = con.CreateCommand();
                command2.CommandText = "SELECT Count(*) FROM Animais";
                int num1 = (Int32)command2.ExecuteScalar();
                int num = 0;
                if (num1 == 0)
                    num = 1;
                else
                {
                    num = (Int32)command1.ExecuteScalar();
                    num++;
                }

                con.Close();
                Cliente m1 = new Cliente(Convert.ToInt32(dataGridView1["id_cliente", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["morada", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["cod_postal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["localidade", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["telemovel", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["email", dataGridView1.CurrentRow.Index].Value.ToString());
                Animal n = new Animal(num, "novo nome", "inserir espécie", "inserir nro chip", "AAAA/MM/DD", "M", null, "", "S");
                frm_ianimal frm6 = new frm_ianimal(m1, n, "novo");
                frm6.ShowDialog();
            }
            else
                MessageBox.Show("Primeiro pesquise e selecione um cliente!", "Selecione o cliente!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void cmdgc_ie_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                int i = this.dataGridView1.CurrentRow.Index;
                string temp_id = this.dataGridView1.Rows[i].Cells["id_cliente"].Value.ToString();

                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT MAX(id_exploracao) FROM Exploracao";
                SqlCommand command2 = con.CreateCommand();
                command2.CommandText = "SELECT Count(*) FROM Exploracao";
                int num1 = (Int32)command2.ExecuteScalar();
                int num = 0;
                if (num1 == 0)
                    num = 1;
                else
                {
                    num = (Int32)command1.ExecuteScalar();
                    num++;
                }

                con.Close();
                Cliente m2 = new Cliente(Convert.ToInt32(dataGridView1["id_cliente", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["morada", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["cod_postal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["localidade", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["telemovel", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["email", dataGridView1.CurrentRow.Index].Value.ToString());
                Exploracao ex3 = new Exploracao(num, "insira um tipo!", 0, Convert.ToInt32(temp_id));

                frm_insexploracao frm7 = new frm_insexploracao(m2, ex3, "novo");
                frm7.ShowDialog();
            }
            else
                MessageBox.Show("Primeiro pesquise e selecione um cliente!", "Selecione o cliente!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
